package com.example.export.config;

import com.example.export.job.DcAppJob;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.util.Objects;

/**
 * @author: dangwen
 * @createDate: 2022/4/4
 */
@Configuration
public class QuartzConfig2 {

    @Bean(name = "jobDetailFirst")
    public JobDetailFactoryBean jobDetailFirst(){
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean() ;
        factoryBean.setJobClass(DcAppJob.class) ;
        return factoryBean;
    }

    @Bean(name = "cronTriggerFirst")
    public CronTriggerFactoryBean cronTriggerFirst(@Qualifier("jobDetailFirst") JobDetailFactoryBean jobDetailFirst){
        CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean() ;
        factoryBean.setJobDetail(Objects.requireNonNull(jobDetailFirst.getObject()));
        String cron = "0/3 * * * * ? ";
        factoryBean.setCronExpression(cron);
        return factoryBean;
    }

    @Bean(name = "schedulerFactoryBean")
    public SchedulerFactoryBean schedulerFactoryBean(@Qualifier("cronTriggerFirst") CronTriggerFactoryBean cronTriggerFactoryBean){
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setTriggers(cronTriggerFactoryBean.getObject());
        return schedulerFactoryBean;
    }

}
